﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Data;
using System.Text;
using System.IO;
using System.Web.UI.DataVisualization.Charting;

namespace OA.Controllers
{
    public class CustomerContributeChartController : Controller
    {
        //
        // GET: /CustomerContributeChart/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CustomerContributeChart()
        {
           
                return View();
        }
        
        //加载会员类型
        public ActionResult LoadMemberType()
        {
            consumeSolutionManager CSManager = new consumeSolutionManager();
            List<consumeSolution>  allName = CSManager.GetAll().ToList();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < allName.Count(); i++)
            {
                sb.Append("<input type=\"checkbox\" id=" + allName[i].id+ " name=\"checkbox\" style=\"margin-left:20px;\" checked=\"true\" /> " + allName[i].solutionName + "");
            }
            sb.Append("<input  type=\"button\"  value=\"搜索\" onclick=\"searchData()\" class=\"button2\" style=\"margin-left:100px;\"/>");
            sb.Append("<input  type=\"button\"  value=\"导出\" onclick=\"toExcel()\" class=\"button2\" style=\"margin-left:50px;\"/>");
            sb.Append("<input  type=\"button\"  value=\"关闭\" onclick=\"closeWindows()\" class=\"button2\" style=\"margin-left:50px;\"/>");
            return Content(sb.ToString());
        }

        //加载数据 
        public ActionResult LoadTabel(FormCollection fc)
        {
            memberInfo MI = new memberInfo();
            memberInfoManager MIManager = new memberInfoManager();
            StringBuilder sb = new StringBuilder();
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int showNUm = Convert.ToInt32(fc["show_Num"]);
            string Id = fc["strId"];
            DataTable dt = MIManager.getTopByBC(showNUm, Id);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i < pageSize)
                {
                    sb.Append("<tr>");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        sb.Append("<td>" + dt.Rows[i][j] + "</td>");
                    }
                    sb.Append("</tr>");
                }
                else
                {
                    break;
                }
            }
            return Content(sb.ToString()); 
        }
        //分页
        public ActionResult newPage(FormCollection fc)
        {
            StringBuilder sb = new StringBuilder();
            int pageNum = Convert.ToInt32(fc["pageNum"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int maxPage = pageNum * pageSize - 1;
            int minPage = maxPage - pageSize + 1;
            memberInfoManager MIManager = new memberInfoManager();
            int show_Num = Convert.ToInt32(fc["show_Num"]);
            string strId = fc["strId"];
            DataTable dt = MIManager.getTopByBC(show_Num, strId);
            for (int i = minPage; i <= maxPage; i++)
            {
                try
                {
                    sb.Append("<tr>");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        sb.Append("<td>" + dt.Rows[i][j] + "</td>");
                    }
                    sb.Append("</tr>");
                }
                catch
                {
                    break;
                }
            }
            return Content(sb.ToString()); 
        }

         //最大页数
        public ActionResult Data_Load_maxPage(FormCollection fc)
        {
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            memberInfoManager MIManager = new memberInfoManager();
            int show_Num = Convert.ToInt32(fc["show_Num"]);
            string strId = fc["strId"];
            DataTable dt = MIManager.getTopByBC(show_Num, strId);
            int count = dt.Rows.Count;
            string oop = Math.Ceiling((double)count / pageSize).ToString();
            return Content(oop);
        }
         

        //获取图表
        public FileResult getChart()
        {
            memberInfoManager MIManager = new memberInfoManager();
            DataTable dt;
          string str=  System.Web.HttpContext.Current.Request.QueryString["strId"];
          string num=  System.Web.HttpContext.Current.Request.QueryString["show_Num"];
          string title = "";
          if (!string.IsNullOrEmpty(str) && !string.IsNullOrEmpty(num))
          {
              dt = MIManager.getTopByBC(Convert.ToInt32(num), str);
              title = "排名前" + num + "贡献图表";
          }
          else
          {
              dt = MIManager.getTopByBC(10,"0");
              title = "排名前10贡献图表";
          }
            Chart CustomerChart = new Chart();
            CustomerChart.Height = 350;
            CustomerChart.Width = 850;
            CustomerChart.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
            CustomerChart.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title(title, Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
            CustomerChart.Titles.Add(t);
            CustomerChart.ChartAreas.Add("view");
            CustomerChart.Series.Add("贡献总额");
            CustomerChart.DataSource = dt;
            CustomerChart.Series["贡献总额"].XValueMember = "memberName";
            CustomerChart.Series["贡献总额"].YValueMembers = "Balance";
            CustomerChart.DataBind();
            CustomerChart.Series["贡献总额"].IsValueShownAsLabel = true;
            CustomerChart.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
           // CustomerChart.BackColor = System.Drawing.Color.Red;
            CustomerChart.BorderlineWidth = 2;
            CustomerChart.BorderColor = System.Drawing.Color.Black;
            CustomerChart.BorderlineDashStyle = ChartDashStyle.Solid;
            CustomerChart.BorderWidth = 1;

            CustomerChart.ChartAreas["view"].AxisX.Interval = 1;
            CustomerChart.ChartAreas["view"].AxisX.MajorGrid.LineWidth = 0;
            CustomerChart.ChartAreas["view"].AxisY.MajorGrid.LineWidth = 0; 
            CustomerChart.Legends.Add("Legend1");
            MemoryStream imageStream = new MemoryStream();
            CustomerChart.SaveImage(imageStream,ChartImageFormat.Png);
            imageStream.Position = 0;
            return new FileStreamResult(imageStream,"image/png");
        }
    }
}
